$NG.AllReady(function (page, { useAction, useBeforeClick,useAttachmentReturn, useValuesChange, useClick, useUpdateRow, subscribe, useUpdateRows }) {
    const config = {
        asrid: '',
        u_lxzt_init: ''
    }
    const user = $NG.getUser()
    const mstform = $NG.getCmpApi("p_form_xtzhs_m")

    // 监听单据附件弹窗返回
    useAttachmentReturn(({ args }) => {
        // 附件弹框点击的确认还是取消
        console.log('status', args[0].closeStatus);
        // 附件的asrSessionGuid
        config.asrid = args[0].asrSessionGuid
        console.log("asrid", args[0].asrSessionGuid);
    });

    useBeforeClick(async () => {
        const result = await tableAttachInit()
        if (!result) {
            return false
        }
        const num = result.data.attachmentRecordList.length
        if (num == 0) {
            $NG.alert("您还没有上传附件，请上传附件！")
            return false
        }
        return true
    }, 'save')

    // 查询附件，找到附件的数量
    async function tableAttachInit() {
        if (!config.asrid) {
            $NG.alert("请上传附件")
            return false
        }
        const origin = location.origin
        const router = "/JFileSrv/reactAttach/tableAttachInit"
        const param = {
            asrSessionGuid: config.asrid,
            busTypeCode: "Disclose_First",
            asrFill: user.userID,
            asrFillName: user.userName,
            asrTable: 'FirstLevelDisclosureFrom',
            asrCode: page.oType == 'add' ? config.asrid : mstform.getValues().phid,
            orgId: user.orgID,
            colAttach: '0',
            uCode: '0001'
        }
        const query = new URLSearchParams(param).toString();
        const url = `${origin}${router}?${query}`;
        try {
            const res = await fetch(url, {
                method: 'GET',
                headers: {
                    "dbToken": '0001'
                }
            });
            if (!res.ok) {
                throw new Error(`HTTP 错误：${res.status}`);
            }
            // 假设接口返回 JSON
            const data = await res.json();
            console.log("返回数据：", data);
            // 根据业务逻辑处理
            if (data.code == 200) {
                return data
            } else {
                console.warn("请求返回异常：", data.message || data);
                $NG.alert("获取附件数量发生异常，请联系管理员！")
                return false
            }
        } catch (err) {
            console.error("请求失败：", err);
            $NG.alert("获取附件数量发生异常，请联系管理员！")
            return false
        }

    }
})